/*
 Copyright 2023 YaoZinan zinan@outlook.com an-archi.cc

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

#ifndef AFEEAF44_4082_43BE_8A63_7573272B7F6C
#define AFEEAF44_4082_43BE_8A63_7573272B7F6C
#include <stdint.h>
#include <string>
#include <vector>

#include "3rd/simpleini/SimpleIni.h"
#include "trace.h"

namespace lrp {
struct LrpsContext {
    int16_t port;
};

#define PROTO_TCP "tcp"

struct TunnelCfg {
    uint16_t port;
    uint16_t r_port;  // 允许重新映射端口
    char     proto[ 8 ];
} __attribute__( ( __packed__ ) );

struct LrpcContext {
    using TunnelRepo = std::vector<TunnelCfg>;

    int16_t     port;
    std::string addr;
    TunnelRepo  tunnels;
};

static int load_config( const char* filename, LrpsContext& ctx );
static int load_config( const char* filename, LrpcContext& ctx );

//-----implementations
int load_config( const char* filename, LrpsContext& ctx ) {
    CSimpleIniA ini;
    TRACE( "ini filename=%s\n", filename );
    auto rc = ini.LoadFile( filename );
    if ( rc < 0 ) return rc;

    CSimpleIniA::TNamesDepend names;
    ini.GetAllSections( names );
    for ( auto& n : names ) {
        TRACE( "section=%s\n", n.pItem );
        if ( strcmp( n.pItem, "common" ) == 0 ) {
            ctx.port = ini.GetLongValue( "common", "port", 0 );
        }
        else {
            TRACE( "unknown section\n" );
        }
    }

    // dump ctx
    TRACE( "port=%d\n", ctx.port );

    return 0;
}

int load_config( const char* filename, LrpcContext& ctx ) {
    CSimpleIniA ini;

    TRACE( "ini filename=%s\n", filename );
    auto rc = ini.LoadFile( filename );

    if ( rc < 0 ) return rc;

    CSimpleIniA::TNamesDepend names;
    ini.GetAllSections( names );

    for ( auto& n : names ) {
        TRACE( "section=%s\n", n.pItem );
        if ( strcmp( n.pItem, "common" ) == 0 ) {
            ctx.port = ini.GetLongValue( "common", "server_port", 0 );
            ctx.addr = ini.GetValue( "common", "server_addr", "" );
        }
        else {
            TunnelCfg cfg;
            cfg.port   = ini.GetLongValue( n.pItem, "local_port", 0 );
            cfg.r_port = ini.GetLongValue( n.pItem, "remote_port", 0 );

            if ( cfg.r_port == 0 ) cfg.r_port = cfg.port;

            auto proto = ini.GetValue( n.pItem, "proto", "" );
            memset( cfg.proto, 0, sizeof( cfg.proto ) );
            strncpy( cfg.proto, proto, std::min( sizeof( cfg.proto ), strlen( proto ) + 1 ) );

            ctx.tunnels.push_back( cfg );
        }
    }

    // dump ctx
    TRACE( "port=%d\n", ctx.port );
    TRACE( "addr=%s\n", ctx.addr.c_str() );

    for ( auto& t : ctx.tunnels ) {
        TRACE( "*tunnel: port=%d r-port=%d proto=%s\n", t.port, t.r_port, t.proto );
    }

    return 0;
}
}  // namespace lrp
#endif /* AFEEAF44_4082_43BE_8A63_7573272B7F6C */
